<template>
	<view>
		<!-- <view class="user-yhgl">
			<text>{{users.length}}个用户</text>
		</view> -->
		<uni-search-bar placeholder="请输入关键词搜索" class="uni-searchbar" :radius="100" cancelButton="none" @confirm="search"
			@clear="clearKeywords"></uni-search-bar>
		<view class="head-container">
			<view class="click-title">
				<text :class="panelStatus[0].visible?'wz':'nr'" @click="clickGood(0)">所有</text>
			</view>
			<view class="click-title">
				<text :class="panelStatus[1].visible?'wz':'nr'" @click="clickGood(1)">申请教师</text>
			</view>
			<view class="click-title">
				<text :class="panelStatus[2].visible?'wz':'nr'" @click="clickGood(2)">教师</text>
			</view>
			<view class="click-title">
				<text :class="panelStatus[3].visible?'wz':'nr'" @click="clickGood(3)">学生</text>
			</view>
			<view class="click-title">
				<text :class="panelStatus[4].visible?'wz':'nr'" @click="clickGood(4)">禁止登录</text>
			</view>
		</view>

		<view v-if="panelStatus[0].visible" class="adminUser">
			<view v-for="(user,index) in users" :key="index" class="kuai1">
				<text class="iconfont icon-sanjiaoxing1 img-menu " @click.stop="clickPopMenu(user)"></text>
				<image :src="'http://localhost:8081/'+ user.avatar" mode="widthFix" class="img"
					@click.stop="clickShowUser(user)"></image>
				<text class="container">{{user.nickName +"["+userTypes[user.type+1]+"]"}}</text>
			</view>
		</view>
		<view v-if="panelStatus[1].visible" class="adminUser">
			<view v-for="(user,index) in users" :key="index" class="kuai1" v-if="user.apply=true">
				<text class="iconfont icon-sanjiaoxing1 img-menu " @click.stop="clickPopMenu(user)"></text>
				<image :src="'http://localhost:8081/'+ user.avatar" mode="widthFix" class="img"
					@click.stop="clickShowUser(user)"></image>
				<text class="container">{{user.nickName}}</text>
			</view>
		</view>
		<view v-if="panelStatus[2].visible" class="adminUser">
			<view v-for="(user,index) in users" :key="index" class="kuai1" v-if="user.type==1">
				<text class="iconfont icon-sanjiaoxing1 img-menu " @click.stop="clickPopMenu(user)"></text>
				<image :src="'http://localhost:8081/'+ user.avatar" mode="widthFix" class="img"
					@click.stop="clickShowUser(user)"></image>
				<text class="container">{{user.nickName}}</text>
			</view>
		</view>
		<view v-if="panelStatus[3].visible" class="adminUser">
			<view v-for="(user,index) in users" :key="index" class="kuai1" v-if="user.type==2">
				<text class="iconfont icon-sanjiaoxing1 img-menu " @click.stop="clickPopMenu(user)"></text>
				<image :src="'http://localhost:8081/'+ user.avatar" mode="widthFix" class="img"
					@click.stop="clickShowUser(user)"></image>
				<text class="container">{{user.nickName}}</text>
			</view>
		</view>
		<view v-if="panelStatus[4].visible" class="adminUser">
			<view v-for="(user,index) in users" :key="index" class="kuai1" v-if="user.type==-1">
				<text class="iconfont icon-sanjiaoxing1 img-menu " @click.stop="clickPopMenu(user)"></text>
				<image :src="'http://localhost:8081/'+ user.avatar" mode="widthFix" class="img"
					@click.stop="clickShowUser(user)"></image>
				<text class="container">{{user.nickName}}</text>
			</view>
		</view>
		<dialog-shell ref="shell" title="用户详情">
			<!-- @confirm="dosth" -->
			<text style="padding-left:15rpx ;padding-top: 15rpx;">{{userDesc}}</text>
		</dialog-shell>
	</view>
</template>
<script>
	const app = getApp()
	let page = 0
	let a = "管理员"
	let b = "教师"
	let c = "学生"
	export default {
		data() {
			return {
				users: [],
				token: app.globalData.token,
				searching: false,
				goSearch: false,
				userTypes: ["禁止登录", "管理员", "教师", "学生", ],
				userDesc: "",
				panelStatus: [{
						visible: true
					}, {
						visible: false
					},
					{
						visible: false
					},
					{
						visible: false
					},
					{
						visible: false
					},
				],
			}
		},
		onLoad() {
			this.getadminUser()
		},
		methods: {

			clickGood(index) {
				let ad = this.panelStatus[index].visible
				this.panelStatus[index].visible = !ad

			},
			clickPopMenu(user) {
				uni.showActionSheet({
					itemList: ['禁止登录', '审批通过', '驳回申请','取消'],
					success: res => {
						if (res.tapIndex == 0) {
							this.userBan(user.id);
						} else if (res.tapIndex == 1) {
							this.userApprove(user.id)
						} else if (res.tapIndex == 2) {
							this.userDecline(user.id)
						}
						else{
							return
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			userBan(id) {
			
				let data = {
					token: app.globalData.token,
					"id": id
				}
				let url = this.$params.host + this.$params.action_admin_ban
				this.$request.postParams(url, data, res => {
					console.log(url)
					this.users.forEach(b => {
						if (b.id == id) {
							b.type = res.data.type
						}
					})
				})
			},
			userApprove(id) {
				let data = {
					token: app.globalData.token,
					"id": id
				}
				let url = this.$params.host + this.$params.action_admin_approve
				this.$request.postParams(url, data, res => {
					console.log(url)
					this.users.forEach(b => {
						if (b.id == id) {
							b.apply = res.data.applyTeacher
							b.type = res.data.type
						}
					})
				})
			},
			userDecline(id) {
				let data = {
					token: app.globalData.token,
					"id": id
				}
				let url = this.$params.host + this.$params.action_admin_decline
				this.$request.postParams(url, data, res => {
					console.log(url)
					this.users.forEach(b => {
						if (b.id == id) {
							b.apply = res.data.applyTeacher
						}
					})
				})
			},
			getadminUser() {
				//获取所有用户
				let url = this.$params.host + this.$params.action_admin
				let data = {
					token: app.globalData.token
				}
				this.$request.get(url, data, res => {
					this.users = res.data.data

					console.log(res);
				})
			},
			clearKeywords() {
				this.searching = false
				this.users = []
				page = 0
				this.getadminUser()

			},
			clearSearch() {
				//清空搜索框
				this.goSearch = false
				this.users = []
				page = 0
				this.getadminUser()
			},
			search(e) {
				let kw = e.value
				if (kw.trim().length == 0) {
					return
				}
				this.goSearch = true
				this.users = []
				this.count = -1
				let url = this.$params.host + this.$params.action_search + kw
				let data = {
					token: app.globalData.token
				}
				this.$request.get(url, data, res => {
					this.users = res.data.data
				})
			},
			clickShowUser(user) {
				if (user.type == 0) {
					user.type = a
				} else if (user.type == 1) {
					user.type=b
				}
				else{
					user.type=c
				}
				this.userDesc = "作者:" + user.nickName + ",联系方式:" + user.email + ",身份:" + user.type
				this.$refs.shell.showDialog()
				// this.$refs.login.show()
			},
		}
	}
</script>

<style>
	@import url("admin.css");
</style>
